#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2017-08-16 13:55:19
# Project: load_data_bili

from pyspider.libs.base_handler import *
import csv
import pymysql
import config as cf
import redis
import error_handler

class Handler(BaseHandler):
    crawl_config = {
    }

    def __init__(self):
        conn = pymysql.connect(**cf.mysql_option)
        cursor = conn.cursor()
        r = redis.StrictRedis(**cf.redis)
        self.conn = conn
        self.cursor = cursor
        self.r = r

    @every(minutes= 4 * 60)
    def on_start(self):
        try:
            qqq = self.r.get("med_flow_bilibili")
            qqq = str(qqq).split(";")
            with open("data_bilibili.csv", "w") as data:
                writer = csv.writer(data)
                for i in qqq:
                    writer.writerow(i.split("|||"))
            self.cursor.execute(
                ("LOAD DATA LOCAL INFILE '{0}' REPLACE INTO TABLE " +
                 "qp.med_flow FIELDS TERMINATED BY ',' ").format("./data_bilibili.csv")
            )
            self.conn.commit()
            self.r.set("med_flow_duanzi","")
        except Exception as e:
            print(e)
            error_handler.handler_error(e)


